
function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}

function addMajorCode(){
	var str='<web:GetAllMajorInfo></web:GetAllMajorInfo>';
	$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/MajorCodeService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			$.each($(xml).find('item'), function(i, n){
				$("#major").append("<option value="+$(this).find("ma_id").text()+">"+$(this).find("ma_majorName").text()+"</option>"); 
            
			});
			
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
	});
}
function loadStu(str,s,flag){
	$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/StudentExtendInfoService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			 var tableHtml = '<tbody>';
			$.each($(xml).find("item"), function(i, n){
				var examplace=$(this).find('stuext_examType').text();
				var examtype=$(this).find('stuext_examPlace').text();
				if(examplace=="0")
					examplace="杭州";
				else if(examplace=="1")
					examplace="宁波";
				if(examtype=="0")
					examtype="自主考试";
				else if(examtype=="1")
					examtype="推荐选拔";
				else if(examtype=="2")
					examtype="考研调剂";
				var li;
				var flagStr;
				if(flag=="0"){	//0笔试
					 li=$(this).find('stuext_roomID').text();
					 flagStr="笔试";
				}else if(flag=="1"){
					li=$(this).find('stuext_auditionGroup').text();
					flagStr="面试";
				}
												
				if(li!=""&&li!="0"){
					//tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd bluebg">';
					tableHtml += '<tr class="bluebg">';
				}else{
					tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
				}
                	//$(this).parent("tr").addClass("bluebg");
                //else tableHtml+='<td>'+flagStr+'</td>';
				
				tableHtml += '<td><input type="checkbox" name="stuext_id" value="'+$(this).find('stuext_id').text()+'"/></td>';
				tableHtml += '<td>' + $(this).find('stuext_id').text() + '</td>';
                tableHtml += '<td>' + examplace + '</td>';
                tableHtml += '<td>' + examtype + '</td>';
                tableHtml += '<td>' + s + '</td>';
                if(flag=="0"){		//笔试
                	tableHtml += '<td>' + $(this).find('stuext_roomID').text() + '</td>';
    				tableHtml += '<td>' + $(this).find('stuext_seatNumber').text() + '</td>';
                }else if(flag=="1"){  //面试
                	tableHtml += '<td>' + $(this).find('stuext_auditionGroup').text() + '</td>';
    				tableHtml += '<td>' + $(this).find('stuext_auditionOrder').text() + '</td>';
                }
				
                tableHtml += '</tr>';
			
			});
			tableHtml += '</tbody>';
            $('#studentlistfoTable').find('tbody').remove().end().append(tableHtml);
			
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	 var xmlObj = $(x.responseText);
             if (xmlObj.find('faultstring').text() == 'oops') {
                 alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                 return;
             }
             else {
                 alert('error:' + x.responseText);
             }
        },
        complete: function(x){
        
        }
	});
}
//function loadStu(str,s){
//	$.ajax({
//        type: "POST",
//        contentType: "text/xml",
//        url: '../../Service/StudentExtendInfoService',
//        data: soapWrapFunc(str),
//        dataType: 'xml',
//		success:function(xml){
//			 var tableHtml = '<tbody>';
//			$.each($(xml).find("item"), function(i, n){
//				var examplace=$(this).find('stuext_examType').text();
//				var examtype=$(this).find('stuext_examPlace').text();
//				if(examplace=="0")
//					examplace="杭州";
//				else if(examplace=="1")
//					examplace="宁波";
//				if(examtype=="0")
//					examtype="自主考试";
//				else if(examtype=="1")
//					examtype="推荐选拔";
//				else if(examtype=="2")
//					examtype="考研调剂";
//				var li=$(this).find('stuext_roomNum').text();
//				
//				tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
//				if(li=="")
//                	tableHtml += '<td><input type="checkbox" name="stuext_id" value="'+$(this).find('stuext_id').text()+'"/></td>';
//                else tableHtml+='<td>已安排</td>';
//				tableHtml += '<td>' + $(this).find('stuext_id').text() + '</td>';
//                tableHtml += '<td>' + examplace + '</td>';
//                tableHtml += '<td>' + examtype + '</td>';
//                tableHtml += '<td>' + s + '</td>';
//				tableHtml += '<td>' + $(this).find('stuext_roomNum').text() + '</td>';
//				tableHtml += '<td>' + $(this).find('stuext_seatNumber').text() + '</td>';
//                tableHtml += '</tr>';
//			
//			});
//			tableHtml += '</tbody>';
//            $('#studentlistfoTable').find('tbody').remove().end().append(tableHtml);
//			
//		},
//		beforeSend: function(xhr){
//            xhr.setRequestHeader("SOAPAction", "\"\"");
//        },
//        error: function(x, e){
//        	 var xmlObj = $(x.responseText);
//             if (xmlObj.find('faultstring').text() == 'oops') {
//                 alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
//                 return;
//             }
//             else {
//                 alert('error:' + x.responseText);
//             }
//        },
//        complete: function(x){
//        
//        }
//	});
//}
function loadRoom(str,s){
			$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/RoomInfoService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			var tableHtmlr = '<tbody>';
			$.each($(xml).find("item"), function(i, n){
				var examplace=$(this).find('ro_examPlace').text();
				var examtype=$(this).find('ro_stuType').text();
				var faceorpen=$(this).find('ro_faceOrPen').text();
				if(examplace=="0")
					examplace="杭州";
				else if(examplace=="1")
					examplace="宁波";
				if(examtype=="0")
					examtype="自主考试";
				else if(examtype=="1")
					examtype="推荐选拔";
				else if(examtype=="2")
					examtype="考研调剂";
				if(faceorpen=="0"){
					faceorpen="笔试";
				}else if(faceorpen=="1"){
					faceorpen="面试";
				}
				tableHtmlr += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
                tableHtmlr += '<td><input type="radio" name="RoomNum" value="'+$(this).find('ro_id').text()+'"/></td>';
                tableHtmlr += '<td>' + $(this).find('ro_id').text() + '</td>';
                tableHtmlr += '<td>' + examplace + '</td>';
                tableHtmlr += '<td>' + examtype + '</td>';
                tableHtmlr += '<td>' + s + '</td>';
                tableHtmlr += '<td>' + faceorpen + '</td>';
                tableHtmlr += '</tr>';
			
			});
			tableHtmlr += '</tbody>';
            $('#roomlistTable').find('tbody').remove().end().append(tableHtmlr);
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	 var xmlObj = $(x.responseText);
             if (xmlObj.find('faultstring').text() == 'oops') {
                 alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                 return;
             }
             else {
                 alert('error:' + x.responseText);
             }
        },
        complete: function(x){
        
        }
	});
}
//安排面试
function setRoomFace(stuID,roomNum){
	//验证报文
	var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
	
	var str='<web:ModifyStudentRoomInfoOfFace>'+userinfo+
	'<id>'+stuID+'</id>'+
	'<room>'+roomNum+'</room>'+
	'</web:ModifyStudentRoomInfoOfFace>';
	$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/StudentExtendInfoService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			alert($(xml).find('return').text());
			//loadStu(str);
			//安排考场后，重新load考生数据
			var examPlace=$.trim($('input[name=examplace]:checked').val());
			var studentType=$.trim($('input[name=studenttype]:checked').val());
			var major=$.trim($('#major').val());
			var s1 = $('#major option[value="'+major+'"]').text();
			var str='<web:GetStudentExtendInfoByExamInfo>'+userinfo+
			'<examPlace>'+examPlace+'</examPlace>'+
			'<studentType>'+studentType+'</studentType>'+
			'<major>'+major+'</major>'+
			'</web:GetStudentExtendInfoByExamInfo>';
			x=str;
			//loadStu(str,s1);
			loadStu(str,s1,1);
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	var xmlObj = $(x.responseText);
            if (xmlObj.find('faultstring').text() == 'oops') {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        },
        complete: function(x){
        
        }
	});
}

function setRoom(stuID,roomNum){
		//验证报文
		var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    
		var str='<web:ModifyStudentRoomInfo>'+userinfo+
		'<id>'+stuID+'</id>'+
		'<room>'+roomNum+'</room>'+
		'</web:ModifyStudentRoomInfo>';
		$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/StudentExtendInfoService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			alert($(xml).find('return').text());
			//loadStu(str);
			//安排考场后，重新load考生数据
			var examPlace=$.trim($('input[name=examplace]:checked').val());
			var studentType=$.trim($('input[name=studenttype]:checked').val());
			var major=$.trim($('#major').val());
			var s1 = $('#major option[value="'+major+'"]').text();
			var str='<web:GetStudentExtendInfoByExamInfo>'+userinfo+
			'<examPlace>'+examPlace+'</examPlace>'+
			'<studentType>'+studentType+'</studentType>'+
			'<major>'+major+'</major>'+
			'</web:GetStudentExtendInfoByExamInfo>';
			x=str;
			//loadStu(str,s1);
			loadStu(str,s1,0);
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	var xmlObj = $(x.responseText);
            if (xmlObj.find('faultstring').text() == 'oops') {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        },
        complete: function(x){
        
        }
	});
}
$(function(){


	


});

/*
 *
 *
 */
$(window).load(function(){
	addMajorCode();
	//搜索
	$('#searchBtn').click(function(){
		//验证报文
	    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
	    
	    var faceorpen=$.trim($('input[name=faceorpen]:checked').val());
	    
		var examPlace=$.trim($('input[name=examplace]:checked').val());
		var studentType=$.trim($('input[name=studenttype]:checked').val());
		var major=$.trim($('#major').val());
		var s1 = $('#major option[value="'+major+'"]').text();
		var str='<web:GetStudentExtendInfoByExamInfo>'+userinfo+
		'<examPlace>'+examPlace+'</examPlace>'+
		'<studentType>'+studentType+'</studentType>'+
		'<major>'+major+'</major>'+
		'</web:GetStudentExtendInfoByExamInfo>';
		x=str;
		//loadStu(str,s1);
		if(faceorpen=="0"){	//笔试
			loadStu(str,s1,0);
		}else if(faceorpen=="1"){	//面试
			loadStu(str,s1,1);
		}
		var str1='<web:GetRoomInfoByExamInfo>'+
		'<examPlace>'+examPlace+'</examPlace>'+
		'<studentType>'+studentType+'</studentType>'+
		'<major>'+major+'</major>'+
		'<faceOrPen>'+faceorpen+'</faceOrPen>'+
		'</web:GetRoomInfoByExamInfo>';
		loadRoom(str1,s1);
	});
	//安排
	$('#setRoomBtn').click(function(){
		//验证报文
	    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
	    //笔试面试
	    var faceorpen=$.trim($('input[name=faceorpen]:checked').val());
	    	    
		var roomNum=$('input[name="RoomNum"]:checked').val();
		var ln=$("input[type='checkbox'][name='stuext_id'][checked=true]").length;
		var ar=new Array();
		ar.length=ln;
		//验证已经选择考场		
		if(typeof(roomNum)=="undefined"){
			alert("对不起，还未指定考场，请返回指定考场");
			return ;
		}
		
		$('input[name="stuext_id"]:checked').each(function(i,n){
			ar[i]=$(this).val();
			//setRoom($(this).val(),roomNum);
			}); 
		//验证考生已经选择考生
		if(ar==""){
			alert("对不起，还未指定考生，请返回指定考生");
			return ;
		}
		
		if(faceorpen=="0"){	//笔试
			
			setRoom(ar,roomNum);
	    }else if(faceorpen=="1"){  	//面试
	    	
	    	setRoomFace(ar,roomNum);
	    }
		
		
		
		
	});
});


